{% extends "counterpoint/admin/base.html" %}

{% load forumlib %}

{% block extrahead %}
{{ block.super }}
    <script type="text/javascript">
      window.addEvent('domready', function(){
        var sortableOptionsBase = {
          initialize: function(){
            this.elements.setStyle('background-color', '');
            this.elements.setStyle('font-size', '10pt');
            this.handles.setStyle('cursor', 'move');
          },
          onStart: function(el){
            el.setStyle('background-color', '#D7D3D2');
          },
          onComplete: function(el){
            el.setStyle('background-color', '');

            var items = el.parentNode.getElements('li');
            var idp, iid;
            if (el.getAttribute('group_id'))
            {
              items = items.filterByAttribute('group_id')
              idp = 'id_group_';
              iid = 'group_id';
            }
            else
            {
              items = items.filterByAttribute('forum_id')
              idp = 'id_forum_';
              iid = 'forum_id';
            }
            for (var i = 0; i < items.length; i++)
            {
              var input = $(idp + items[i].getAttribute(iid) + '-ordering');
              input.setAttribute('value', i + 1);
            }
          }
        };
        var groupOptions = $merge(sortableOptionsBase, {handles: $$(".group_title")});
        new Sortables($("group_list"), groupOptions);
        var groups = $$(".group_row");
        for (var i = 0; i < groups.length; i++)
        {
          var forumOptions = $merge(sortableOptionsBase, {handles: groups[i].getElements(".forum_title")});
          new Sortables(groups[i], forumOptions);
        }
      });
    </script>
{% endblock %}

{% block admin_header %}Forum Management{% endblock %}

{% block content %}
{% regroup object_list|dictsort:"parent_group.ordering" by parent_group as grouped %}
    <form action="" method="post">
      <ul id="group_list" style="padding-left: 0px; list-style-type: none;">
{% for group in grouped %}
        <li group_id="{{ group.grouper.id }}" style="padding: 2px 2px 5px 2px;">
          {% ordering_for group.grouper %}
          <span class="group_title">{{ group.grouper.title }}</span>
          [<a href="{% url cp_admin_group_edit group.grouper.id %}">Edit</a>]
          <ul class="group_row" style="padding-left: 20px;">
  {% for forum in group.list|dictsort:"ordering" %}
            <li forum_id="{{ forum.id }}" style="padding: 2px;">
              {% ordering_for forum %}
              <span class="forum_title">{{ forum }}</span>
              [<a href="{% url cp_admin_forum_edit group.grouper.id,forum.id %}">Edit</a>]<br />
              {{ forum.description }}
            </li>
  {% endfor %}
          </ul>
          <ul style="padding-left: 20px;">
            <li style="padding: 2px;">
              <a href="{% url cp_admin_forum_add group.grouper.id %}">Add Forum</a>
            </li>
          </ul>
        </li>
{% endfor %}
      </ul>
      <ul style="padding-left: 0px; list-style-type: none;">
        <li style="padding: 2px 2px 5px 2px;">
          <a href="{% url cp_admin_group_add %}">Add Forum Group</a>
        </li>
      </ul>
      <input style="margin-top: 10px;" type="submit" value="Save Ordering" />
    </form>
{% endblock %}
